<?php
/**
 * Created by PhpStorm.
 * User: lee
 * Date: 2018/4/13
 * Time: 上午11:11
 *
 * 功能：系统定时维护过期的优惠券，标识状态为过期状态
 * 版本：v1.0
 *
 *
 * 规则：每天0点1分
 *
 */

ini_set('display_errors', 1);
error_reporting(-1);
require_once '/data/wwwroot/g_vendor/autoload.php';

use Biz\model\activity\CouponRecord as mCouponRecord;


if (isset($argv[1])) {
    $stopMiunte = date('Y-m-d H:i:00',strtotime($argv[1]));
} else {
    $stopMiunte = date('Y-m-d H:i:00', time());
}

$startMiunte = date('Y-m-d H:i:00',strtotime($stopMiunte)-30);

$where = [
    'stop_time[>]' => $startMiunte,
    'stop_time[<=]' => $stopMiunte,
    'status'    => mCouponRecord::COUPON_STATUS_UNUSED,
];

try {
    //取出待处理的数据
    $couponList = mCouponRecord::select("*", $where);

    if (empty($couponList)) {
        throw new \Exception("当前时刻没有待处理的优惠券数据");
    }

    foreach ($couponList as $coupon) {

        try {

            mCouponRecord::update(['status'=>mCouponRecord::COUPON_STATUS_EXPIRED],['coupon_vid'=>$coupon['coupon_vid'],'status'=>mCouponRecord::COUPON_STATUS_UNUSED]);

        } catch (\Exception $e) {

            echo sprintf("%s    时刻:%s   coupon_vid:%s   异常：%s  \n", date('Y-m-d H:i:s'),$stopMiunte, $coupon['coupon_vid'],json_encode([
                'e_file'=>$e->getFile(),
                'e_line'=>$e->getLine(),
                'e_msg'=>$e->getMessage()
            ],JSON_UNESCAPED_UNICODE));

        }finally{
            echo sprintf("%s    时刻:%s    coupon_vid:%s   sql日志：%s   \n",date('Y-m-d H:i:s'),$stopMiunte,$coupon['coupon_vid'],mCouponRecord::sqlLogs());

            mCouponRecord::clearLogs();
        }
    }
} catch (\Exception $e) {

    echo sprintf("%s    %s 时刻:%s    sql日志：%s\n", date('Y-m-d H:i:s'), $e->getMessage(), $stopMiunte,mCouponRecord::sqlLogs());

    mCouponRecord::clearLogs();


}






